import request from './request'

// 上传文件上七牛
/**
 * options: Object
 * url: 'https://upload-z2.qiniup.com' 上传到服务器路径
 * name: 'file'
 * filePath: path
 * header: 'Content-Type": "multipart/form-data'
 * formData: {token}
 * 
 */

export default function upload(options) {
  new Promise((resolve, reject) => {
    console.log('七牛上传: ', options)
    //检查有没有上传token, 兼容自己找token的情况
    if (!options.token) {
      const params = {
        url: 'http://39.108.160.36:8890/api/qiniu/token',
      }
      request._request(params).then(res => {
        console.log('返回token结果: ', res)
        if (!res.data.cmd || !response.data.result.token) {
          throw new Error('获取文件上传令牌失败')
        }
        options.token = res.data.result.token
        wx.uploadFile({
          options,
          success: function(res) {
            resolve(res);
          },
          fail: function(res) {
            reject(res);
          }
        });
      });
    };
  });
}